package com.atguigu.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/*
* 配置后置处理器
* 主要是因为redis存储键时候乱码
* 然后定义他的key存储规则
* */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //判断bean可不可以赋值为RedisTemplate(就是判断)
       if( RedisTemplate.class.isAssignableFrom(bean.getClass())){
           //设置key存储规则
            RedisTemplate redisTemplate=(RedisTemplate) bean;
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            return redisTemplate;
        }
       return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
